﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Admin.NET.Application.Service;
using Furion.FriendlyException;
using System.Collections.Generic;
using System.Linq;

namespace Admin.NET.Application;
/// <summary>
/// 车型服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class car_carService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<car_car> _rep;
    public car_carService(SqlSugarRepository<car_car> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<car_carOutput>> Page(car_carInput input)
    {
        var query = _rep.Context.Queryable<car_tag>().Where(m=>m.IsDelete==false).LeftJoin<car_tag>((ca, pca) => ca.pid == pca.Id)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.carname), ca => ca.title.Contains(input.carname.Trim()))
                    .WhereIF(input.pid > 0, ca => ca.pid == input.pid)
                
                    .Select((ca, pca) => new car_carOutput
                    {

                         carname=ca.title,
                          Id=ca.Id,
                           pid=ca.pid,
                            pname=pca.title,
                    });


;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }


    /// <summary>
    /// 获取车型id列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "car_carPidDropdown"), HttpGet]
    public List<TreeModel> car_carPidDropdown()
    {
        var lists = _rep.Context.Queryable<car_car>().Where(m => m.pid == 0 && m.IsDelete==false).ToList();

        

        List<TreeModel> reList = new List<TreeModel>();
        foreach (var item in lists)
        {
            reList.Add(new TreeModel()
            {
                label = item.carname,
                value = item.Id,
                //children = lists.Where(m => m.pid == item.Id).Select(u => new TreeModelItem
                //{
                //    label = u.carname,
                //    value = u.Id,

                //}).ToList(),
            });
        }
        return reList;
        //return await _rep.Context.Queryable<car_car>().GroupBy(m=>m.pid)
        //        .Select(u => new
        //        {
        //            Label = u.carname,
        //            Value = u.Id
        //        }
        //        ).ToListAsync();
    }


    /// <summary>
    /// 增加车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(Addcar_carInput input)
    {
        var entity = input.Adapt<car_car>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(Deletecar_carInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(Updatecar_carInput input)
    {
        var entity = input.Adapt<car_car>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取车型
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<car_car> Get([FromQuery] QueryByIdcar_carInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取车型列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<car_carOutput>> List([FromQuery] car_carInput input)
    {
        return await _rep.AsQueryable().Select<car_carOutput>().ToListAsync();
    }





}

